<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#696969; '>' Controls if the current HTML page will be rendered to PDF or as a normal page</span>
<span style='color:#800000; font-weight:bold; '>Private</span> convertToPdf <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Boolean</span> <span style='color:#808030; '>=</span> <span style='color:#0f4d75; '>False</span>

<span style='color:#800000; font-weight:bold; '>Protected</span> <span style='color:#800000; font-weight:bold; '>Sub</span> convertToPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>ByVal</span> sender <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Object</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>ByVal</span> e <span style='color:#800000; font-weight:bold; '>As</span> EventArgs<span style='color:#808030; '>)</span>
    <span style='color:#696969; '>' The current ASP.NET page will be rendered to PDF when its Render method will be called by framework</span>
    convertToPdf <span style='color:#808030; '>=</span> <span style='color:#0f4d75; '>True</span>
<span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>Sub</span>

<span style='color:#800000; font-weight:bold; '>Protected</span> <span style='color:#800000; font-weight:bold; '>Overrides</span> <span style='color:#800000; font-weight:bold; '>Sub</span> Render<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>ByVal</span> writer <span style='color:#800000; font-weight:bold; '>As</span> HtmlTextWriter<span style='color:#808030; '>)</span>
    <span style='color:#800000; font-weight:bold; '>If</span> convertToPdf <span style='color:#800000; font-weight:bold; '>Then</span>
        <span style='color:#696969; '>' Get the current page HTML string by rendering into a TextWriter object</span>
        <span style='color:#800000; font-weight:bold; '>Dim</span> outTextWriter <span style='color:#800000; font-weight:bold; '>As</span> TextWriter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>New</span> StringWriter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>
        <span style='color:#800000; font-weight:bold; '>Dim</span> outHtmlTextWriter <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> HtmlTextWriter<span style='color:#808030; '>(</span>outTextWriter<span style='color:#808030; '>)</span>
        <span style='color:#800000; font-weight:bold; '>MyBase</span><span style='color:#008c00; '>.</span>Render<span style='color:#808030; '>(</span>outHtmlTextWriter<span style='color:#808030; '>)</span>

        <span style='color:#696969; '>' Obtain the current page HTML string</span>
        <span style='color:#800000; font-weight:bold; '>Dim</span> currentPageHtmlString <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>String</span> <span style='color:#808030; '>=</span> outTextWriter<span style='color:#008c00; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>

        <span style='color:#696969; '>' Create a HTML to PDF converter object with default settings</span>
        <span style='color:#800000; font-weight:bold; '>Dim</span> htmlToPdfConverter <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> HtmlToPdfConverter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>

        <span style='color:#696969; '>' Set license key received after purchase to use the converter in licensed mode</span>
        <span style='color:#696969; '>' Leave it not set to use the converter in demo mode</span>
        htmlToPdfConverter<span style='color:#008c00; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#0000e6; '>"4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="</span>

        <span style='color:#696969; '>' Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed</span>
        <span style='color:#696969; '>' Set this property to 0 if you don't need to wait for such asynchcronous operations to finish</span>
        htmlToPdfConverter<span style='color:#008c00; '>.</span>ConversionDelay <span style='color:#808030; '>=</span> <span style='color:#008c00; '>2</span>

        <span style='color:#696969; '>' Use the current page URL as base URL</span>
        <span style='color:#800000; font-weight:bold; '>Dim</span> baseUrl <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>String</span> <span style='color:#808030; '>=</span> HttpContext<span style='color:#008c00; '>.</span>Current<span style='color:#008c00; '>.</span>Request<span style='color:#008c00; '>.</span>Url<span style='color:#008c00; '>.</span>AbsoluteUri

        <span style='color:#696969; '>' Convert the current page HTML string to a PDF document in a memory buffer</span>
        <span style='color:#800000; font-weight:bold; '>Dim</span> outPdfBuffer<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Byte</span> <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>ConvertHtml<span style='color:#808030; '>(</span>currentPageHtmlString<span style='color:#808030; '>,</span> baseUrl<span style='color:#808030; '>)</span>

        <span style='color:#696969; '>' Send the PDF as response to browser</span>

        <span style='color:#696969; '>' Set response content type</span>
        Response<span style='color:#008c00; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Content-Type"</span><span style='color:#808030; '>,</span> <span style='color:#0000e6; '>"application/pdf"</span><span style='color:#808030; '>)</span>

        <span style='color:#696969; '>' Instruct the browser to open the PDF file as an attachment or inline</span>
        Response<span style='color:#008c00; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Content-Disposition"</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>String</span><span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>Format</span><span style='color:#808030; '>(</span><span style='color:#0000e6; '>"attachment; filename=Convert_Current_Page.pdf; size={0}"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#008c00; '>.</span>Length<span style='color:#008c00; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span>

        <span style='color:#696969; '>' Write the PDF document buffer to HTTP response</span>
        Response<span style='color:#008c00; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span>

        <span style='color:#696969; '>' End the HTTP response and stop the current page processing</span>
        Response<span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>End</span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>
    <span style='color:#800000; font-weight:bold; '>Else</span>
        <span style='color:#800000; font-weight:bold; '>MyBase</span><span style='color:#008c00; '>.</span>Render<span style='color:#808030; '>(</span>writer<span style='color:#808030; '>)</span>
    <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>If</span>
<span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>Sub</span>
</pre>
</span>